
#include <signal.h>
#include "RemoteCtrol.h"
int main(int argc, char *argv[]){

	int sockfd, new_fd;
	struct sockaddr_in server_addr;
	struct sockaddr_in client_addr;
	int sin_size, portnumber;
	char client_command[100];

	if ( argc != 2 ){
		fprintf(stderr,"usage:%s portnumber",argv[0]);
		exit(1);
	}

	if ((portnumber = atoi(argv[1]))<0){
		fprintf(stderr,"usage:%s portnumber",argv[0]);
		exit(1);
	}

	if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
		fprintf(stderr, "socket error:%s a", strerror(errno));
		exit(1);
	}
	memset(&server_addr,0,sizeof(struct sockaddr_in));
	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	server_addr.sin_port = htons(portnumber);

	if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr)) == -1){
		fprintf(stderr, "bind error:%s a", strerror(errno));
		exit(1);
	}

	if(listen(sockfd, 5) == -1){
		fprintf(stderr, "listen error:%s a", strerror(errno));
		exit(1);
	}
	printf("start to listening on port:%d\n",portnumber);

	while(1){
		sin_size = sizeof(struct sockaddr_in);
		if((new_fd = accept(sockfd, (struct sockaddr *)(&client_addr),&sin_size))== -1){
			fprintf(stderr, "accept error:%s a",strerror(errno));
			exit(1);
		}

		fprintf(stderr,"server get connetion from %s\n",inet_ntoa(client_addr.sin_addr));
		
/*
		int i=0;
		char buf[100];
		memset(buf,0,100);
		for( i = 0; i < 12; i++){
			sprintf(buf, "%d a b c d e f g h\r\n",i);
			printf("write data:%s",buf);
			my_write(new_fd,buf,sizeof(buf));
		//	sleep(1);
		}
		*/
		int pid = fork();
		if( pid  == 0){
			signal(SIGCHLD,SIG_DFL);
			do_loop(new_fd);
		}
		else if( pid > 0 ){
			printf("close the connection\n");
			close(new_fd);
		}
		else{
			printf("fork() error");
			exit(1);
		}
	}
	exit(0);
}


void do_loop(int sockfd){
	char combuf[COMMAND_LEN];//recive message from clinet
	char local_combuf[20];//one piece of command should have less than 20 chars
	char *pcombuf;
	int created = 0;
	process_args_t arg;
	arg.sockfd = sockfd;
	while(1){
		memset(combuf,0,COMMAND_LEN);
		int res = recv_msg(sockfd,combuf,COMMAND_LEN);
		if( res < 0 ) {
			printf("error in read commond from client!close the connection\n");
			break;
		}
		
		pcombuf = combuf;
		while (pcombuf - combuf < COMMAND_LEN && *pcombuf!='\0'){ 
			if(strstr(pcombuf,"INTERVAL") == pcombuf){
				printf("receive a valid command:%s",combuf);
				arg.interval = atoi(combuf+9);
				printf("inter:%d\n",arg.interval);
				if(created==1)
					concel_thread();
				printf("start  thread!\n");
				pthread_create(&proc_th,NULL,process_main,(void *)&arg);
				created=1;
			}
			if(strstr(pcombuf,"END") == pcombuf){
				printf("receive a valid command:%s",combuf);
				concel_thread();
				break;
			}
			if(strstr(pcombuf,"KILL") == pcombuf){
				printf("receive a valid command:%s",combuf);
			}
			while((*pcombuf++) != '\n');
		}
	}
	if(pthread_kill(proc_th,0) == 0)
		concel_thread();
}
	

void concel_thread(){

	int err = pthread_cancel(proc_th);
	void* res;
	if ( err != 0 ){
		printf("concel_thread:can't cancel the thread %s\n",strerror(err));
		exit(-1);
	}
	err = pthread_join(proc_th,&res);
	if ( err != 0 ){
		printf("conel_thread:can't join the thread %s\n",strerror(err));
		exit(-1);
	}
	if ( res == PTHREAD_CANCELED ){
		printf("thread %u has been canceled \n",(unsigned int )res);
	}
	else{
		printf("error");
	}
}

